package com.zxx.flowerstore.utils.utils;

import org.apache.commons.lang3.StringUtils;

/**
 * @ClassName StringUtil
 * @Author Admin
 * @Date 2021/3/20 11:51
 * @Description StringUtil
 * @Version 1.0
 */
public class StringUtil {

    private static final String PATTERN = "上海市天津市北京市重庆市";

    /**
     * @author: xurs
     * @date: 19-6-4
     * @Description: 获取备注中的协议号
     */
    public static String getOrderNoOnRemark(String remark) {
        //全角转半角
        String str = ToDBC(remark);
        //替换非字母数字的字符为,
        str = str.replaceAll("[^a-zA-Z0-9]", ",");
        //把多余的,合并为一个
        str = str.replaceAll("[,]+", ",");
        //把第一个,去掉
        if ((str.startsWith(","))) {
            str = str.substring(1);
        }
        return str;
    }

    /**
     * @author: xurs
     * @date: 19-6-4
     * @Description: 全角字符串转半角
     */
    public static String ToDBC(String input) {
        char c[] = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == '\u3000') {
                c[i] = ' ';
            } else if (c[i] > '\uFF00' && c[i] < '\uFF5F') {
                c[i] = (char) (c[i] - 65248);
            }
        }
        String returnString = new String(c);
        return returnString;
    }


    /**
     * @author: xurs
     * @date: 19-6-4
     * @Description: 去除市
     */
    public static String removeShi(String newString) {
        if (StringUtils.isBlank(newString)) {
            return "";
        }
        String[] strArrs = newString.split("");
        StringBuilder sb = new StringBuilder();
        for (String str : strArrs) {
            if (!StringUtils.isWhitespace(str)) {
                sb.append(str);
            }
        }
        newString = sb.toString();
        if (PATTERN.contains(newString)) {
            newString = newString.replaceAll("市", "");
        }
        return newString;
//        boolean isShanghai = newString.contains("上海");
//        if (isShanghai){
//            //包含
//            return newString = replace(newString,"市");
//        }
//        boolean isBeijing = newString.contains("北京");
//        if (isBeijing){
//            return newString = replace(newString,"市");
//        }
//        boolean isTianjing = newString.contains("天津");
//        if (isTianjing){
//            return newString = replace(newString,"市");
//        }
//        return newString;

    }

    static String replace(String string, String remString) {
        string.replace(string, remString);

        string = string.replace(remString, "");
        return string;
    }

    /**
     * 去除前后空格比较
     *
     * @param str1
     * @param str2
     * @return boolean
     * @author zhengxin
     * @date 2020/9/8 10:01
     * @version: 1.0
     */
    public static boolean trimAndEquals(String str1, String str2) {
        return StringUtils.defaultIfBlank(str1, "").trim()
                .equals(StringUtils.defaultIfBlank(str2, "").trim());
    }

    /**
     * 是否是http地址
     *
     * @param path
     * @return boolean
     * @author zhengxin
     * @date 2020/10/12 11:21
     * @version: 1.0
     */
    public static boolean isHttpUrl(String path) {
        return StringUtils.startsWithIgnoreCase(path, "http");
    }

    /**
     * 手机号脱敏
     *
     * @param phoneNumber
     * @return java.lang.String
     * @author zhengxin
     * @date 2020/12/9 17:42
     * @version: 1.0
     */
    public static String desensitizedPhoneNumber(String phoneNumber) {
        if (StringUtils.isNotEmpty(phoneNumber)) {
            phoneNumber = phoneNumber.replaceAll("(\\w{3})\\w*(\\w{4})", "$1****$2");
        }
        return phoneNumber;
    }

    /**
     * 身份证脱敏
     *
     * @param idNumber
     * @return java.lang.String
     * @author zhengxin
     * @date 2020/12/14 17:17
     * @version: 1.0
     */
    public static String desensitizedIdNumber(String idNumber) {
        if (StringUtils.isNotBlank(idNumber) && idNumber.length() > 7) {
            StringBuffer regex = new StringBuffer("$1");
            int asterisk = idNumber.length() - 7;
            for (int i = 0; i < asterisk; i++) {
                regex.append("*");
            }
            regex.append("$2");
            return idNumber.replaceAll("(\\w{3})\\w*(\\w{4})", regex.toString());
        }
        return idNumber;
    }


    public static void main(String[] args) {
//        String orderNoOnRemark = getOrderNoOnRemark(":::TCDH1905290009,TCDP1905280029");
//        System.out.println(orderNoOnRemark);
        String s = removeShi("上 海      ");
        System.out.println(s.length());
        System.out.println(s);

    }

}
